﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Demo.Services.Contracts.DTO.Infos;

namespace Demo.Web.Catalogs
{
    public partial class ResourceTecnology1 : System.Web.UI.UserControl
    {
        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
            }
        }

        public void ShowWindow()
        {
            
            TecnologyWindow.Show();                                    
            List<TecnologyInfo> assignedTecnologyList = null;
            List<TecnologyInfo> unAssignedTecnologyList = null;

            if (Session["Assigned"] != null)
            {
                assignedTecnologyList = (List<TecnologyInfo>)Session["Assigned"];
                gpAssigned.GetStore().DataSource = assignedTecnologyList;
                gpAssigned.GetStore().DataBind();
            }
            if (Session["UnAssigned"] != null)
            {
                unAssignedTecnologyList = (List<TecnologyInfo>)Session["UnAssigned"];
                gpUnAssigned.GetStore().DataSource = unAssignedTecnologyList;
                gpUnAssigned.GetStore().DataBind();                
            }                       
        }

        protected void Asignar(object sender, DirectEventArgs e)
        {     
            string jsonA = e.ExtraParams["Assigned"];
            string jsonUn = e.ExtraParams["UnAssigned"];

            SetTecnologies("Assigned", jsonA);
            SetTecnologies("UnAssigned", jsonUn);           
            TecnologyWindow.Close();
        }

        private void SetTecnologies(string session, string json)
        {
            List<TecnologyInfo> tecnologyList = new List<TecnologyInfo>();
            Dictionary<string, string>[] tecnologies = JSON.Deserialize<Dictionary<string, string>[]>(json);
            foreach (Dictionary<string, string> row in tecnologies)
            {
                string desc = row["Description"];
                int id = Convert.ToInt32(row["Id"]);
                TecnologyInfo tecnology = new TecnologyInfo();
                tecnology.Description = desc;
                tecnology.Id = id;
                tecnologyList.Add(tecnology);                
            }
            Session[session] = tecnologyList;
        }
        
    }
}